package com.yuanfengPlus.customerExpense.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yuanfengPlus.customerExpense.dao.ICustomerExpenseDetailsMapper;
import com.yuanfengPlus.customerExpense.dao.ICustomerExpenseMapper;
import com.yuanfengPlus.customerExpense.model.CustomerExpenseDetailsInfo;
import com.yuanfengPlus.customerExpense.model.CustomerExpenseInfo;
import com.yuanfengPlus.customerExpense.service.ICustomerExpenseService;
import com.yuanfengPlus.supplierExpense.model.SupplierExpenseInfo;
import com.yuanfengPlus.utils.PageResult;
import com.yuanfengPlus.utils.Result;
import com.yuanfengPlus.utils.SnowflakeIdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class CustomerExpenseServiceImpl implements ICustomerExpenseService {
    @Resource
    private ICustomerExpenseMapper icem;
    @Resource
    private ICustomerExpenseDetailsMapper icedm;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<String> insert(CustomerExpenseInfo customerExpenseInfo) {
        if (customerExpenseInfo.getCustomerExpenseDetailsInfoList() == null)
            return Result.failure(500, "采购信息不能为空");

        // 计算总金额
        double totalAmount = 0.0;
        for (CustomerExpenseDetailsInfo customerExpenseDetailsInfo : customerExpenseInfo.getCustomerExpenseDetailsInfoList()) {
            totalAmount += customerExpenseDetailsInfo.getValue();
        }
        customerExpenseInfo.setCustomer_expenses_total_value(totalAmount);

        // 插入供应商费用信息
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(30);
        customerExpenseInfo.setCustomer_expenses_ID(snowflakeIdWorker.nextId());
        customerExpenseInfo.setCustomer_expenses_operate_time(new java.sql.Timestamp(System.currentTimeMillis()));
        customerExpenseInfo.setCustomer_expenses_review_time(new java.sql.Timestamp(System.currentTimeMillis()));
        Integer insert = icem.insert(customerExpenseInfo);
        if (insert <= 0)
            return Result.failure(500, "客户费用信息插入失败");
        // 批量插入采购信息
        List<CustomerExpenseDetailsInfo> customerExpenseDetailsInfoList = customerExpenseInfo.getCustomerExpenseDetailsInfoList();
        for (CustomerExpenseDetailsInfo customerExpenseDetailsInfo : customerExpenseDetailsInfoList){
            customerExpenseDetailsInfo.setId(customerExpenseInfo.getCustomer_expenses_ID());
            icedm.insert(customerExpenseDetailsInfo);
        }
        return Result.success("客户费用信息插入成功");
    }

    @Override
    public Result<String> update(CustomerExpenseInfo customerExpenseInfo) {
        customerExpenseInfo.setCustomer_expenses_review_time(new java.sql.Timestamp(System.currentTimeMillis()));
        Integer delete = icedm.delete(customerExpenseInfo.getCustomer_expenses_ID());
        if (delete <= 0)
            return Result.failure(500, "客户费用信息修改失败");
        // 插入新的供应商信息
        List<CustomerExpenseDetailsInfo> customerExpenseDetailsInfoList = customerExpenseInfo.getCustomerExpenseDetailsInfoList();
        if (customerExpenseDetailsInfoList != null && !customerExpenseDetailsInfoList.isEmpty()){
            for (CustomerExpenseDetailsInfo customerExpenseDetailsInfo : customerExpenseDetailsInfoList){
                customerExpenseDetailsInfo.setId(customerExpenseInfo.getCustomer_expenses_ID());
                Integer insert = icedm.insert(customerExpenseDetailsInfo);
                if (insert <= 0)
                    return Result.failure(500, "客户费用信息修改失败");
            }
        }
        // 计算总金额
        double totalAmount = 0.0;
        for (CustomerExpenseDetailsInfo customerExpenseDetailsInfo : customerExpenseInfo.getCustomerExpenseDetailsInfoList()){
            totalAmount += customerExpenseDetailsInfo.getValue();
        }
        customerExpenseInfo.setCustomer_expenses_total_value(totalAmount);
        Integer update = icem.update(customerExpenseInfo);
        if (update <= 0)
            return Result.failure(500, "客户费用信息修改失败");
        return Result.success("客户费用信息修改成功");
    }

    @Override
    public PageResult select(Map<String, Object> params) {
        // 检查并设置默认值
        Integer pageNum = (Integer) params.getOrDefault("pageNum", 1);
        Integer pageSize = (Integer) params.getOrDefault("pageSize", 10);

        // 确保 pageNum 和 pageSize 不为 null
        if (pageNum == null || pageSize == null) {
            throw new IllegalArgumentException("pageNum 和 pageSize 不能为 null");
        }

        PageHelper.startPage(pageNum, pageSize);

        // 确保 ilocm 不为 null
        if (icem == null) {
            throw new IllegalStateException("ilocm 未正确初始化");
        }

        Page<CustomerExpenseInfo> page = icem.selectAll(params);

        // 确保 page 不为 null
        if (page == null) {
            throw new IllegalStateException("查询结果为空");
        }
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Result<CustomerExpenseInfo> selectById(String customer_expenses_ID) {
        CustomerExpenseInfo customerExpenseInfo = icem.selectById(customer_expenses_ID);
        System.out.println("customerExpenseInfo"+customerExpenseInfo);
        if (customerExpenseInfo == null) {
            return Result.failure(500, "客户费用信息查询失败");
        }
        return Result.success(customerExpenseInfo);
    }
}
